<template>
    <view>
        <!-- pages/setDoorList/setDoorList.wxml -->

        <view class="container" :style="isIpx ? 'padding-bottom:168rpx' : 'padding-bottom:120rpx'">
            <block v-if="doorList.length">
                <view class="list">
                    <!-- <view class="item doing" wx:for="{{doorList}}" wx:key="item" class="{{item.status==0?'item disabled':item.status==1?'item undo':item.status==2?'item clean':item.status==3?'item doing':'item yuyue'}}"> -->
                    <view class="item doing" v-for="(item, index) in doorList" :key="index">
                        <view class="top">
                            <view class="name">
                                {{ item.roomName }}
                                <text class="type">
                                    （{{
                                        item.type === 0
                                            ? '特价包'
                                            : item.type === 1
                                            ? '小包'
                                            : item.type === 2
                                            ? '中包'
                                            : item.type === 3
                                            ? '大包'
                                            : item.type === 4
                                            ? '豪包'
                                            : '商务包'
                                    }}）
                                </text>
                            </view>
                            <view class="price color-attention">￥{{ item.price }}/小时</view>
                        </view>

                        <view class="tags">
                            <block v-for="(labelitem, index1) in magic.split(item.label)" :key="index1">
                                <view class="tag">{{ labelitem }}</view>
                            </block>
                        </view>

                        <view class="info">
                            <view class="line" style="width: 100%">
                                <label>禁用时间：</label>
                                <text v-if="item.banTimeStart">{{ item.banTimeStart }} - {{ item.banTimeEnd }}</text>
                            </view>
                            <view class="delRoom" @tap="delRoom" :data-id="item.roomId">删除</view>
                        </view>

                        <view class="info">
                            <view class="line">
                                <label>当前状态：</label>
                                <text>{{ item.status == 0 ? '禁用' : item.status == 1 ? '空闲中' : item.status == 2 ? '待清洁' : item.status == 3 ? '使用中' : '已预约' }}</text>
                            </view>
                            <view class="line">
                                <text v-if="item.orderTime">{{ item.orderTime }}</text>
                            </view>
                        </view>

                        <van-divider contentPosition="center" customStyle="color: #108ee9;" @tap.native="foldTap($event, { index: item.roomId })" :data-index="item.roomId">
                            {{ foldIndex === item.roomId ? '隐藏' : '显示管理功能' }}
                        </van-divider>

                        <view :class="'btns ' + (foldIndex === item.roomId ? 'show' : '')">
                            <view class="line2">
                                <view class="btn warning" @tap="disableRoom" :data-roomid="item.roomId">启用禁用</view>
                                <navigator class="btn warning" :url="'../setDoorInfo/setDoorInfo?roomId=' + item.roomId + '&&storeId=' + storeId">修改信息</navigator>
                                <view class="btn yellow" :data-src="item.qrCode" @tap="previewImage">房间预定码</view>
                                <view class="btn yellow" :data-src="item.renewCode" @tap="previewImage">房间续费码</view>
                            </view>
                            <view class="line2">
                                <view class="btn origin" @tap="queryLockPwd" :data-lockdata="item.lockData" :data-roomid="item.roomId">查询锁密码</view>
                                <view class="btn origin" @tap="setLockPwdShowFun" :data-lockdata="item.lockData" :data-roomid="item.roomId">设置锁密码</view>
                                <view class="btn origin" @tap="addLockCard" :data-lockdata="item.lockData" :data-roomid="item.roomId">添加锁卡片</view>
                                <view class="btn origin" @tap="setLockGateWay" :data-lockdata="item.lockData" :data-roomid="item.roomId">锁数据查询</view>
                            </view>
                            <view class="line2">
                                <view class="btn warning" @tap="clearAndFinish" :data-roomid="item.roomId">设置空闲</view>
                                <view class="btn warning" @tap="finishOrder" :data-roomid="item.roomId">结束订单</view>
                                <view class="btn red" @tap="openBlueDoor" :data-lockdata="item.lockData" :data-roomid="item.roomId">蓝牙开锁</view>
                                <view class="btn red" @tap="openDoor" :data-roomid="item.roomId">开电</view>
                                <view class="btn red" @tap="closeDoor" :data-roomid="item.roomId">关电</view>
                            </view>
                        </view>
                    </view>
                </view>
            </block>
            <block v-else>
                <view class="nodata">暂无数据</view>
            </block>
        </view>
        <!-- 底部按钮 -->
        <navigator :url="'../setDoorInfo/setDoorInfo?storeId=' + storeId" :class="'bottom bg-primary ' + (isIpx ? 'fix-iphonex-button' : '')">新增房间</navigator>

        <van-dialog use-slot title="设置门锁密码" :show="setLockPwdShow" show-cancel-button confirm-button-text="确认" @confirm="confirmSetLockPwd">
            <view class="dialog">
                <view class="item" style="align-items: center">
                    <label>新管理员密码：</label>
                    <input v-model="lockPwd" type="number" maxlength="8" style="border: 1px solid var(--main-color)" placeholder="请输入6-8纯数字密码" />
                </view>
            </view>
        </van-dialog>
    </view>
</template>
<script module="magic" lang="wxs" src="@/utils/magic.wxs"></script>
<script>
// pages/setDoorList/setDoorList.js
const app = getApp();
var http = require('../../../utils/http');
var lock = require('../../../utils/lock.js');
export default {
    data() {
        return {
            storeId: 0,
            doorList: [],
            isIpx: app.globalData.isIpx ? true : false,
            foldIndex: -1,
            setLockPwdShow: false,
            lockPwd: '',
            lockData: '',
            labelitem: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        this.setData({
            storeId: Number(options.storeId)
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getDoorList();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        foldTap(e, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(e, _dataset);
            /* ---处理dataset end--- */
            console.log(e);
            const { target: { dataset = {} } = {} } = e;
            this.setData({
                foldIndex: this.foldIndex === dataset.index ? -1 : dataset.index
            });
        },

        // 获取房间列表
        getDoorList: function () {
            let that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/store/getRoomInfoList/' + that.storeId,
                    '1',
                    'get',
                    {
                        storeId: that.storeId
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                doorList: info.data
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 开房间门
        openDoor: function (e) {
            let roomId = e.currentTarget.dataset.roomid;
            let that = this;
            uni.showModal({
                title: '提示',
                content: '确定打开房间门吗',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/openRoomDoor/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '开门成功',
                                            icon: 'success'
                                        });
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        // 关房间门
        closeDoor: function (e) {
            let roomId = e.currentTarget.dataset.roomid;
            let that = this;
            uni.showModal({
                title: '提示',
                content: '确定关闭房间门吗',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/closeRoomDoor/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '关门成功',
                                            icon: 'success'
                                        });
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        openBlueDoor: function (e) {
            let that = this;
            let lockData = e.currentTarget.dataset.lockdata;
            if (lockData) {
                //本地蓝牙开锁
                lock.blueDoorOpen(lockData);
            } else {
                uni.showToast({
                    title: '未使用密码锁'
                });
            }
        },

        disableRoom: function (e) {
            let roomId = e.currentTarget.dataset.roomid;
            let that = this;
            uni.showModal({
                title: '提示',
                content: '确定修改房间状态吗？',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/disableRoom/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                        that.getDoorList();
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        testYunlaba: function (e) {
            let roomId = e.currentTarget.dataset.roomid;
            let that = this;
            uni.showModal({
                title: '提示',
                content: '房间喇叭将播放预设内容，确定操作吗？',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/testYunlaba/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        // 清洁并结单
        clearAndFinish: function (e) {
            let roomId = e.currentTarget.dataset.roomid;
            let that = this;
            uni.showModal({
                title: '注意提示',
                content: '注意！！！房间状态将变为空闲！并立即关电！如果有进行中的订单，订单将会被结束！请谨慎确认房间当前状态后再操作！！！',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/clearAndFinish/' + roomId,
                                '1',
                                'get',
                                {},
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    // console.info('返回111===');
                                    // console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                        that.getDoorList();
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        previewImage(e) {
            var currentUrl = e.currentTarget.dataset.src; //获取当前点击图片链接
            uni.previewImage({
                urls: [currentUrl]
            });
        },

        // 结单
        finishOrder: function (e) {
            let roomId = e.currentTarget.dataset.roomid;
            let that = this;
            uni.showModal({
                title: '注意提示',
                content: '注意！！！房间状态将变为待清洁！并立即关电！如果有进行中的订单，订单将会被结束！请谨慎确认房间当前状态后再操作！！！',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/finishRoomOrder/' + roomId,
                                '1',
                                'get',
                                {},
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    // console.info('返回111===');
                                    // console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                        that.getDoorList();
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        delRoom: function (e) {
            let roomId = e.currentTarget.dataset.id;
            let that = this;
            uni.showModal({
                title: '注意提示',
                content: '请确认是否删除该房间！！！该房间不能存在未完成的订单！',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/deleteRoomInfo/' + roomId,
                                '1',
                                'post',
                                {},
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    // console.info('返回111===');
                                    // console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                        that.getDoorList();
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        queryLockPwd: function (e) {
            let that = this;
            let lockData = e.currentTarget.dataset.lockdata;
            if (lockData) {
                lock.queryLockPwd(lockData);
            } else {
                uni.showToast({
                    title: '未使用密码锁'
                });
            }
        },

        addCard: function (e) {
            let that = this;
            let lockData = e.currentTarget.dataset.lockdata;
            if (lockData) {
                lock.addCard(lockData);
            } else {
                uni.showToast({
                    title: '未使用密码锁'
                });
            }
        },

        setLockPwdShowFun: function (e) {
            let that = this;
            var lockData = e.currentTarget.dataset.lockdata;
            if (lockData) {
                that.setData({
                    setLockPwdShow: true,
                    lockData: lockData
                });
            } else {
                uni.showToast({
                    title: '未使用密码锁',
                    icon: 'error'
                });
            }
        },

        confirmSetLockPwd: function (e) {
            var that = this;
            var lockData = that.lockData;
            console.log('lockData');
            console.log(lockData);
            if (lockData) {
                if (!that.lockPwd || that.lockPwd < 100000) {
                    uni.showToast({
                        title: '密码不合法',
                        icon: 'error'
                    });
                } else {
                    lock.setLockPwd(lockData, that.lockPwd);
                    that.setData({
                        setLockPwdShow: false,
                        lockData: ''
                    });
                }
            } else {
                uni.showToast({
                    title: '未使用密码锁'
                });
            }
        },

        addLockCard: function (e) {
            var that = this;
            let lockData = e.currentTarget.dataset.lockdata;
            if (lockData) {
                lock.addCard(lockData);
            } else {
                uni.showToast({
                    title: '未使用密码锁'
                });
            }
        },

        setLockGateWay: function (e) {
            var that = this;
            let lockData = e.currentTarget.dataset.lockdata;
            if (lockData) {
                lock.setLockGateWay(lockData);
            } else {
                uni.showToast({
                    title: '未使用密码锁'
                });
            }
        }
    }
};
</script>
<style>
@import './setDoorList.css';
</style>
